// pages/publish/publish.js
import dayjs from '../../miniprogram_npm/dayjs/index'
import config from '../../utils/config'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        passangerSum: 1,
        minDate: new Date().getTime(),
        departureTime: '',
        Departure: "",
        Destination: '',
        showDate: false,
        message: '',
        userId: '',
        timestamp: '',
        avator:'',
        nickName:''
    },
    passangerSum(e) {
        this.setData({
            passangerSum: e.detail,
        })
    },
    openDate() {
        this.setData({
            showDate: true
        })
    },
    publish() {
        const data = {
            passangerSum: this.data.passangerSum,
            timestamp: this.data.timestamp,
            Departure: this.data.Departure,
            Destination: this.data.Destination,
            message: this.data.message,
            userId: this.data.userId,
            avator:this.data.avator,
            nickName:this.data.nickName
        }
        wx.request({
            url: `${config.baseUrl}/publish`,
            method: 'POST',
            data: data,
            success: (res) => {
                console.log(res);
                wx.showToast({
                  title: res.data.msg,
                })
            },
            fail: (err) => {
                console.log(err);
            }
        })
    },
    selectDeparture() {
        let that = this
        wx.chooseLocation({
            success(res) {
                console.log(res);
                that.setData({
                    Departure: res.name
                })
                console.log(that.data.Departure);
            }
        }
        )
    },
    selectDestination() {
        let that = this
        wx.chooseLocation({
            success(res) {
                console.log(res);
                that.setData({
                    Destination: res.name
                })
                console.log(that.data.Destination);
            }
        }
        )
    },
    getMessage(e) {
        this.setData({
            message: e.detail
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.getStorage({
            "key": "userInfo",
            success: (res) => {
                console.log(res);
                this.setData({
                    isLogin: res.data.isLogin,
                    userId:res.data.userId,
                    avator:res.data.avator,
                    nickName:res.data.nickName
                })
            }
        })


    },
    selectDate(e) {
        console.log(e.detail);
        const departureTime = dayjs(e.detail).format('YYYY年MM月DD日HH时mm分');
        console.log(departureTime);
        this.setData({
            departureTime: departureTime,
            timestamp: e.detail
        })
        this.closeDate()
    },
    closeDate() {
        this.setData({
            showDate: false
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       
        this.getTabBar().init();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})